import { getMyInfo,
  getUserBrief,
  getMyDetail,
  updateUser,
  updateUsername ,
  registerEmail,
  addTag,
  removeTag
} from '@/services/user';
  import { message } from 'antd';


const UserModel = {
  namespace: 'user',
  state: {
    myInfo:{},
    
  },
  effects: {
    *getMyInfo(_, { call, put }) {
      const response = yield call(getMyInfo);
      if(response) {
        yield put({
          type: 'setMyInfo',
          payload: response.data,
        });
      }
    },
    *getMyDetail({_}, { call, put }){
      const response = yield call(getMyDetail);
      if(response) {
        yield put({
          type: 'setMyDetal',
          payload: response.data,
        });
      }
    },

    *getUserBrief({payload}, { call, put }){
      const response = yield call(getUserBrief,payload);
      if(response) {
        yield put({
          type: 'setUserBrief',
          payload: response.data,
        });
      }
    },
    
    *update({payload}, { call }){
      const response = yield call(updateUser,payload);
      if(response && response.success) {
        message.success("修改成功！")
      }
    },

    *updateUsername({payload}, { call }){
      const response = yield call(updateUsername,payload);
      if(response && response.success) {
        message.success("修改成功！")
      }
      return response.success;
    },

    *registerEmail({payload}, { call }){
      const response = yield call(registerEmail,payload);
      if(response && response.success) {
        message.success("注册成功！")
      }
      return response.success;
    }


  },
  reducers: {
    setMyInfo(state, action) {
      return { ...state, myInfo: action.payload || {} };
    },
    setMyDetal(state, action){
      return { ...state, myDetail: action.payload || {} };
    },
    setUserBrief(state, action){
      return { ...state, userBrief: action.payload || {} };
    },
    
  },
};
export default UserModel;
